package com.ypd.show.service.impl;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ypd.common.core.page.PageDomain;
import com.ypd.common.core.page.TableSupport;
import com.ypd.common.utils.SecurityUtils;
import com.ypd.show.domain.Comment;
import com.ypd.show.domain.Statistic;
import com.ypd.show.domain.vo.ArticleCommentVo;
import com.ypd.show.domain.vo.ArticleVo;
import com.ypd.show.service.ICommentService;
import com.ypd.show.service.IStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ypd.show.mapper.ArticleMapper;
import com.ypd.show.domain.Article;
import com.ypd.show.service.IArticleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章操作Service业务层处理
 * 
 * @author ye
 * @date 2025-01-02
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IStatisticService statisticService;
    @Autowired
    private ICommentService commentService;

    /**
     * 查询文章操作
     * 
     * @param id 文章操作主键
     * @return 文章操作
     */
    @Override
    public ArticleCommentVo selectArticleById(Long id)
    {
        //每次查询文章详情的时候,会查询对应的点击量和评论
        //同时,每次点击之后都会增加点击量
        ArticleCommentVo articleCommentVo = new ArticleCommentVo();
        Article article = articleMapper.selectArticleById(id);
        BeanUtil.copyProperties(article,articleCommentVo);
        Statistic statistic = statisticService.selectStatisticByArticleId(id);
        statistic.setHits(statistic.getHits()+1);
        statisticService.updateStatistic(statistic);
        articleCommentVo.setStatistic(statistic);
        //导入评论
        articleCommentVo.setComments(commentService.selectCommentList(Comment.builder().articleId(id).build()));
        return articleCommentVo;
    }


    /**
     * 查询文章操作列表
     * 
     * @param article 文章操作
     * @return 文章操作
     */
    @Override
    public PageInfo<ArticleVo> selectArticleList(Article article)
    {
        //进行多表查询,查找出文章对应的点击量,对文章和点击量进行包装成一个对象,total重新获取
        //进行返回


        /** 获取分页*/
        PageDomain jsonPageDomain = TableSupport.buildPageRequest();
        Integer  pageNum = jsonPageDomain.getPageNum();
        Integer  pageSize = jsonPageDomain.getPageSize();
        if(pageNum==null || pageSize==null){
            throw new RuntimeException("未获取到分页参数值");
        }
        PageHelper.startPage(pageNum,pageSize);


        List<Article> articles = articleMapper.selectArticleList(article);

        PageInfo articleInfo = new PageInfo<>(articles);
        List<ArticleVo> articleVos = new ArrayList<>();
        //查询对应点击量
        for (Article article1 : articles) {
            ArticleVo articleVo = new ArticleVo();
            BeanUtil.copyProperties(article1,articleVo);
            articleVo.setStatistic(statisticService.selectStatisticList(Statistic.builder().articleId(article1.getId()).build()).get(0));
            articleVos.add(articleVo);
        }
        // 将封装后的列表放到分页对象中
        articleInfo .setList(articleVos);
        return articleInfo;
    }

    /**
     * 新增文章操作
     * 
     * @param article 文章操作
     * @return 结果
     */
    @Override
    @Transactional
    public int insertArticle(Article article)
    {
        //同时要创建一条statistic对象存储点击量.从base中获取到当前登录用户id
        article.setUserId(SecurityUtils.getUserId());
        int x = articleMapper.insertArticle(article);
        if (x>0){
            Statistic statistic = new Statistic( article.getId(), 0L, 0L);
            statisticService.insertStatistic(statistic);
        }
        return x;
    }

    /**
     * 修改文章操作
     * 
     * @param article 文章操作
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArticle(Article article)
    {
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章操作
     * 默认执行
     * 
     * @param ids 需要删除的文章操作主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteArticleByIds(Long[] ids)
    {
        //删除文章的同时也要删除点击量和对应评论
        //根据文章ID查找对应的点击量和评论
        for (Long id : ids) {
            //删除点击量
            statisticService.deleteStatisticById(statisticService.selectStatisticList(Statistic.builder().articleId(id).build()).get(0).getId());
            //删除评论(根据文章id执行删除操作)
            commentService.deleteCommentByArticleId(id);
        }
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章操作信息
     * 
     * @param id 文章操作主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteArticleById(Long id)
    {
        return articleMapper.deleteArticleById(id);
    }
}
